﻿var Aquamarine = Aquamarine || {};

Aquamarine.OpenFileDialog = function (fileExtensions) {
    var self = this;

    var _fileInput = document.createElement("input");
    _fileInput.setAttribute("type", "file");
    _fileInput.setAttribute("multiple", "");
    _fileInput.setAttribute("accept", fileExtensions.join());
    _fileInput.style.position = "absolute";
    _fileInput.style.top = "-999px";
    _fileInput.style.visibility = "hidden";

    document.body.appendChild(_fileInput);

    self.open = function () {
        _fileInput.click();
    };

    self.addEventListener = function(type, listener, useCapture) { 
        _fileInput.addEventListener(type, listener, useCapture);
    };

    self.dispatchEvent = function (evt) {
        _fileInput.dispatchEvent(evt);
    };

    _fileInput.addEventListener("change",  function () {
        var changeEvent = new CustomEvent("changelist", { detail: { files: _fileInput.files } });
        self.dispatchEvent(changeEvent);
    });
};